package com.cn.jdbcutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@JdbcConfig(driver = "com.mysql.jdbc.Driver", password = "root", url = "jdbc:mysql:///day13", username = "root")
public class JdbcUtils {
	
	    private static final String driverName;//必须加final，否则能够通过反射得到
	    private static final String url;
	    private static final String user;
	    private static final String password;
	    
	    static{
	       
	    	JdbcConfig jc = JdbcUtils.class.getAnnotation(JdbcConfig.class);
	        driverName = jc.driver();
	        url = jc.url();
	        user = jc.username();
	        password = jc.password();
	    }
	     
	    static{
	        try {
	            Class.forName(driverName);
	        } catch (ClassNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	    }
	    public static Connection getConnection() throws Exception {
	        Connection conn = (Connection) DriverManager.getConnection(url, user, password);
	        return conn;
	    }
	    public static void closeResource(ResultSet rs,Statement st,Connection con){
	         
	        try {
	            if(rs!=null){
	                rs.close();
	            }
	            if(st!=null){
	                st.close();
	            }
	            if(con!=null){
	                con.close();
	            }
	        } catch (SQLException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	    }
	    public static void main(String[] args) {
			try {
				System.out.println(JdbcUtils.getConnection());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

